namespace ScrumSprintMonitor.ServerAdapter.TFS.TemplateAdaptors
{
	#region #using Directives

	using Infrastructure;

	using Isolation.TeamFoundationServer;

	#endregion

	public class VSTSScrumWorkItemAdapter : CoreWorkItemAdapter
	{
		public VSTSScrumWorkItemAdapter(IWorkItem workItem)
			: base(workItem)
		{
		}

		protected override void RetrieveType()
		{
			switch (WorkItem.Type)
			{
				case "BackLogItem":
					Type = WorkItemType.SprintBacklogItem;
					break;
				case "Defect":
					Type = WorkItemType.Defect;
					break;
				case "User Story":
					Type = WorkItemType.UserStory;
					break;
				default:
					Type = WorkItemType.Unknown;
					break;
			}
		}

		protected override void RetrieveState()
		{
			switch (WorkItem.State)
			{
				case "Closed":
				case "Resolved":
					State = WorkItemState.Done;
					break;
				case "Active":
					State = WorkItemState.InProgress;
					break;
				default:
					State = WorkItemState.NotDone;
					break;
			}
		}

		protected override void RetrieveRemainingWork()
		{
			if (Type == WorkItemType.SprintBacklogItem)
			{
				var remainingWork = WorkItem["Microsoft.VSTS.Scheduling.RemainingWork"];
				RemainingWork = (remainingWork == null ? double.NaN : (double) remainingWork);
			}
			else
			{
				RemainingWork = 0.0;
			}
		}

		protected override void RetrieveDoneWork()
		{
			if (Type == WorkItemType.SprintBacklogItem)
			{
				var completedWork = WorkItem["Microsoft.VSTS.Scheduling.CompletedWork"];
				DoneWork = (completedWork != null ? (double) completedWork : 0.0);
			}
			else
			{
				DoneWork = 0.0;
			}
		}
	}
}